<script lang="ts" setup>
  import { ref, watch, onMounted, onUnmounted } from 'vue';
  import MEcharts from '/@/components/MEcharts/MEcharts.vue';
  import { color } from 'echarts';
  const props = defineProps({
    actWidth: Array<any>,
    setWidth: Number,
    setWidth2: Number,
    xdata: Array<any>,
    seriesName: String,
  });

  const chartRef = ref();
  const option = ref<any>({
    grid: {
      top: '8%',
      left: '8%',
      right: '8%',
      bottom: '20%',
    },
    animation: false,
    tooltip: {
      trigger: 'axis',
    },
    xAxis: {
      data: [],
      type: 'category',
      axisLabel: {
        color: '#fff',
      },
      axisTick: {
        show: false,
      },
      axisLine: {
        show: false,
      },
      z: 10,
    },
    yAxis: {
      color: '#fff',
      name: 'mm',
      nameTextStyle: {
        color: '#fff',
        fontSize: 12,
      },
      boundaryGap: ['0', '10%'],
      axisLine: {
        show: true,
      },
      axisTick: {
        show: true,
      },
      axisLabel: {
        color: '#fff',
      },
      splitLine: {
        show: false,
      },
      scale: true,
    },
    visualMap: {
      show: false,
      pieces: [
        {
          gte: 0,
          lte: 1000,
          color: '#00FF00',
        },
      ],
      outOfRange: {
        color: '#FD0100',
      },
    },
    series: [
      {
        type: 'line',
        name: `${props.seriesName}`,
        data: [],
        lineStyle: {
          width: 1,
        },
        itemStyle: {},
        force: {
          layoutAnimation: false,
        },
        symbolSize: 1,
        smooth: true,
        markLine: {
          symbol: 'none',
          data: [
            {
              yAxis: 0,
              lineStyle: {
                // color: '#FFD700',
                color: '#fff',
                width: 1,
              },
              label: {
                formatter: '目标1',
                // color: '#FFD700',
                color: '#fff',
              },
            },
            {
              yAxis: 0,
              lineStyle: {
                // color: '#00FF00',
                color: '#fff',
                width: 1,
              },
              label: {
                formatter: '目标2',
                // color: '#00FF00',
                color: '#fff',
              },
            },
          ],
        },
      },
    ],
  });

  const updateChart = () => {
    option.value.xAxis.data = props.xdata;
    option.value.series[0].data = props.actWidth;
    option.value.visualMap.pieces[0].lte = props.setWidth ?? 0;

    // 更新两条目标线
    if (props.setWidth !== undefined && props.setWidth !== null && props.setWidth2 !== 0) {
      option.value.series[0].markLine.data[0].yAxis = props.setWidth;
    } else {
      option.value.series[0].markLine.data[0].yAxis = undefined;
    }

    if (props.setWidth2 !== undefined && props.setWidth2 !== null && props.setWidth2 !== 0) {
      option.value.series[0].markLine.data[1].yAxis = props.setWidth2;
    } else {
      option.value.series[0].markLine.data[1].yAxis = undefined;
    }

    const actWidth = props.actWidth ?? [0];
    const setWidth = props.setWidth ?? 0;
    const setWidth2 = props.setWidth2 ?? 0;
    option.value.yAxis.min = Math.min(...actWidth, setWidth, setWidth2) - 1;
    option.value.yAxis.max = Math.max(...actWidth, setWidth, setWidth2) + 1;
  };

  watch(
    () => {
      return props.actWidth?.length;
    },
    () => {
      updateChart();
    }
  );

  onMounted(() => {
    // 确保在 DOM 加载完成后更新图表
    setTimeout(() => {
      updateChart();
    }, 0);

    // 添加窗口大小变化监听
    window.addEventListener('resize', updateChart);
  });

  onUnmounted(() => {
    // 移除窗口大小变化监听
    window.removeEventListener('resize', updateChart);
  });
</script>

<template>
  <div ref="chartRef" class="chart-container">
    <m-echarts :option="option"></m-echarts>
  </div>
</template>

<style scoped>
  .chart-container {
    width: 100%;
    height: 100%;
    min-height: 11vh;
  }
</style>
